<?php

/**
* send_image.php
*
* Send file to browser 
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once("framework/config.php");
require_once("framework/UserErrorHandler.class.php");
require_once("framework/ExceptionHandler.class.php");
require_once('MimeType.class.php');
require_once('Image/Transform.php');
require_once("framework/Wurfl.class.php");

if ( !isset($_GET['id']) || !strlen($_GET['id']) )
{
    trigger_error("send_image: Incorrect file name!", E_USER_ERROR);
    die();
} 


$fileName = basename( $_GET['id'] );
if (  !strlen($fileName) )
{
    trigger_error("send_image: Incorrect file name!", E_USER_ERROR);
    die();
}

//create transform driver object
$it = Image_Transform::factory('GD');

if ( PEAR::isError ( $it ) )
{
    trigger_error("send_image: ".$it->getMessage(), E_USER_ERROR); 
    die();
}


$options = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
$status = $it->load( $options['upload_dir'].$fileName ) ;
//load the original file
if ( PEAR::isError ( $status ) )
{
    trigger_error("send_image: ".$status->getMessage(), E_USER_ERROR); 
    die();
}

$it->scaleByX(  Wurfl::getWidth() );

$mt = new MimeType();

header('Content-type: '.$mt->get($fileName));
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('X-new-size: '. $it->new_x.'x'.$it->new_y); 
$it->display();

?>
